//
//  FJEditorInputView.m
//  OnionHome
//
//  Created by fengjie on 2019/10/15.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "FJEditorInputView.h"

@interface FJEditorInputView ()
/**<#属性#>*/
@property (nonatomic,strong)UIView*topView;
/**样式选择列表*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
/**分割线、打赏视图*/
@property (nonatomic,strong)UIView*lineView;

@end

@implementation FJEditorInputView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = KWhiteColor;
        [self createUI];
    }
    return self;
}
-(void)createUI{
    [self addSubview:self.topView];
    [self addSubview:self.lineView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(44);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}
-(void)setType:(FJEditorInputViewType)type{
    _type = type;
    if (type == FJEditorInputViewStyle) {
        self.topView.hidden = NO;
        self.listViewTool.listView.hidden = NO;
        self.lineView.hidden = YES;
    }else{
        self.topView.hidden = YES;
        self.listViewTool.listView.hidden = YES;
        self.lineView.hidden = NO;
    }
}
-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        kWeakSelf(weakSelf);
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([FJEditorInputCell class]) identifier:NSStringFromClass([FJEditorInputCell class]) dataArray:self.dataArray delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            
            FJEditorInputCell * cell = (FJEditorInputCell *)TempCell;
            cell.model = (FJEditorInputStyleModel *)TempData;
            
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            BlockSafeRun(weakSelf.selectCellBlock,(FJEditorInputStyleModel *)TempData);
        }];
        _listViewTool.listView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16);
        _listViewTool.listView.separatorColor = UIColorFromRGB(0xE9EEE7);
    }
    return _listViewTool;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
        FJEditorInputStyleModel * model1 = [[FJEditorInputStyleModel alloc] initWithTitle:@"大标题" TitleColor:UIColorFromRGB(0x273323) TitleFont:BOLDSYSTEMFONT(16) FontValue:24];
        FJEditorInputStyleModel * model2 = [[FJEditorInputStyleModel alloc] initWithTitle:@"小标题" TitleColor:UIColorFromRGB(0x273323) TitleFont:BOLDSYSTEMFONT(14) FontValue:16];
        FJEditorInputStyleModel * model3 = [[FJEditorInputStyleModel alloc] initWithTitle:@"正文" TitleColor:UIColorFromRGB(0x273323) TitleFont:SYSTEMFONT(14) FontValue:14];
        FJEditorInputStyleModel * model4 = [[FJEditorInputStyleModel alloc] initWithTitle:@"引用" TitleColor:UIColorFromRGB(0x273323) TitleFont:SYSTEMFONT(14) FontValue:14];
        FJEditorInputStyleModel * model5 = [[FJEditorInputStyleModel alloc] initWithTitle:@"·无序列表" TitleColor:UIColorFromRGB(0x273323) TitleFont:SYSTEMFONT(14) FontValue:14];

        [_dataArray addObject:model1];
        [_dataArray addObject:model2];
        [_dataArray addObject:model3];
        [_dataArray addObject:model4];
        [_dataArray addObject:model5];

    }
    return _dataArray;
}
-(UIView*)topView{
    if(!_topView){
        _topView=[UIView new];
        _topView.backgroundColor = UIColorFromRGB(0xF5F7F5);
        
        UILabel * label = UILabel.new;
        label.textColor = UIColorFromRGB(0x273323);
        label.font = SYSTEMFONT(14);
        label.text = @"样式";
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"完成" forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateNormal];
        button.titleLabel.font = SYSTEMFONT(12);
        kWeakSelf(weakSelf);
        [button addActionHandler:^{
            BlockSafeRun(weakSelf.completeBlock);
        }];
        
        [_topView addSubview:label];
        [_topView addSubview:button];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
        }];
        
    }
    return _topView;
}
-(UIView*)lineView{
    if(!_lineView){
        _lineView=[UIView new];
        
        UIImageView * oneImage = UIImageView.new;
        oneImage.backgroundColor = KGrayColor;
        
        UILabel * oneLabel = UILabel.new;
        oneLabel.textColor = UIColorFromRGB(0x959E97);
        oneLabel.font = SYSTEMFONT(12);
        oneLabel.text = @"分割线";
        
        UIImageView * twoImage = UIImageView.new;
        twoImage.backgroundColor = KGrayColor;
        
        UILabel * twoLabel = UILabel.new;
        twoLabel.textColor = UIColorFromRGB(0x959E97);
        twoLabel.font = SYSTEMFONT(12);
        twoLabel.text = @"打赏";
        
        [_lineView addSubview:oneImage];
        [_lineView addSubview:oneLabel];
        [_lineView addSubview:twoImage];
        [_lineView addSubview:twoLabel];

        [oneImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.mas_equalTo(20);
            make.width.height.mas_equalTo(64);
        }];
        [oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(oneImage);
            make.top.mas_equalTo(oneImage.mas_bottom).offset(5);
        }];
        [twoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(oneImage.mas_right).offset(20);
            make.top.mas_equalTo(20);
            make.width.height.mas_equalTo(64);
        }];
        [twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(twoImage);
            make.top.mas_equalTo(twoImage.mas_bottom).offset(5);
        }];

    }
    return _lineView;
}
@end


@implementation FJEditorInputStyleModel

-(instancetype)initWithTitle:(NSString *)title TitleColor:(UIColor *)titleColor TitleFont:(UIFont *)titleFont FontValue:(NSInteger)fontValue{
    self = [super init];
    if (self) {
        self.title = title;
        self.titleColor = titleColor;
        self.titleFont = titleFont;
        self.fontValue = fontValue;
    }
    return self;
}

@end


@interface FJEditorInputCell ()
/**<#属性#>*/
@property (nonatomic,strong)UILabel*titleLabel;

@end

@implementation FJEditorInputCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}
-(void)setModel:(FJEditorInputStyleModel *)model{
    _model = model;
    self.titleLabel.text = model.title;
    self.titleLabel.textColor = model.titleColor;
    self.titleLabel.font = model.titleFont;
}
-(void)createUI{
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
}
-(UILabel*)titleLabel{
    if(!_titleLabel){
        _titleLabel=[UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
@end
